import '../../css/signIn/signInCSS.css';

//登录按钮点击函数
window.clickLogin = function clickLogin(e){
    e.preventDefault();
    console.log(111);
    let username = $("#username").val();
    let password = $("#password").val();

    if(username === ''|| password === ''){
        $("#messages").text("用户名或密码不能为空!");
        $("#messages").removeClass("invisible");
        return;
    }

    $("#messages").addClass("invisible");
    let formDatas = new FormData();
    formDatas.append('username',username);
    formDatas.append('password',password);

    //异步请求登录
    fetch(`http://localhost:8022/user/checkLogin`,{
        method:'POST',
        mode: 'cors',
        body: formDatas
    })
    .then((response)=>{
        if(response.ok){
            return response.json();
        }else{
            console.log("NetWork Error");
        }
    })
    .then((data)=>{
        console.log(data);
        //登录成功处理逻辑
        if(data.status === 1){
            let userId = data.data;
            sessionStorage.setItem('isLogin',true);
            sessionStorage.setItem('username',username);
            sessionStorage.setItem('userId',userId);
            window.location.href = 'index.html';
        }else{
            $("#messages").text("用户名或密码错误!");
            $("#messages").removeClass("invisible");
        }
    })
    .catch((error)=>{
        console.log(error);
    })
    .finally(()=>{
        console.log("处理完成");
    })
}

//注册按钮点击函数
window.clickSignUp = function clickSignUp(e){
    e.preventDefault();
    //跳转到注册按钮
    window.location.href = 'signUp.html';
}